﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.HS.App.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.ReportPlugin.GXD
{
    [Description("GXD:核算单据查询报表插件_扩展"), HotUpdate]
    public class DzHS_HSBillReport : HS_HSBillReport
    {
        private String[] customRptTempTableNames;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            IDBService dBService = ServiceHelper.GetService<IDBService>();
            customRptTempTableNames = dBService.CreateTemporaryTableName(this.Context, 1);
            String strTable = customRptTempTableNames[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);
            StringBuilder sb = new StringBuilder();
            string strSql = @"/*dialect*/select t1.*, CASE T1.FSrcBillType 
            WHEN '采购入库单' THEN A1.F_UFII_Remarks 
            WHEN '采购退料单' THEN A2.FDESCRIPTION 
            WHEN '盘亏单' THEN A4.FNote 
            WHEN '盘盈单' THEN A5.FNote 
            WHEN '其他出库单' THEN A6.FNote 
            WHEN '其他入库单' THEN A7.FNote 
            WHEN '生产补料单' THEN A88.FDESCRIPTION 
            WHEN '生产领料单' THEN A99.FDESCRIPTION 
            WHEN '生产入库单' THEN A100.FDESCRIPTION 
            WHEN '生产退库单' THEN A110.FDESCRIPTION 
            WHEN '生产退料单' THEN A120.FDESCRIPTION 
            WHEN '委外补料单' THEN A130.FDESCRIPTION  
            WHEN '委外超耗单' THEN A140.FDESCRIPTION  
            WHEN '委外领料单' THEN A150.FDESCRIPTION 
            WHEN '委外退料单' THEN A160.FDESCRIPTION 
            WHEN '销售出库单' THEN A17.FNote  
            WHEN '销售退货单' THEN A18.FNote 
            WHEN '形态转换单' THEN A19.FNote
            WHEN '直接调拨单' THEN A20.FNote
            WHEN '组装拆卸单' THEN A21.FNote 
            ELSE '' END FNote 
            into {0}  FROM {1} t1 
            left join t_STK_InStock A1 on t1.FBILLNO = a1.FBILLNO 
            left join t_PUR_MRB a2 on t1.FBILLNO = a2.FBILLNO 
            left join T_STK_STKCOUNTLOSS a4 on t1.FBILLNO = a4.FBILLNO 
            left join T_STK_STKCOUNTGAIN a5 on t1.FBILLNO = a5.FBILLNO 
            left join T_STK_MISDELIVERY a6 on t1.FBILLNO = a6.FBILLNO 
            left join T_STK_MISCELLANEOUS a7 on t1.FBILLNO = a7.FBILLNO 
            left join T_PRD_FEEDMTRL a8 on t1.FBILLNO = a8.FBILLNO 
            LEFT JOIN T_PRD_FEEDMTRL_L A88 ON A88.FID=A8.FID AND A88.FLOCALEID=2052
            left join T_PRD_PICKMTRL a9 on t1.FBILLNO = a9.FBILLNO 
            LEFT JOIN T_PRD_PICKMTRL_L A99 ON A99.FID=A9.FID AND A99.FLOCALEID=2052
            left join T_PRD_INSTOCK a10 on t1.FBILLNO = a10.FBILLNO 
            LEFT JOIN T_PRD_INSTOCK_L A100 ON A100.FID=A10.FID AND A100.FLOCALEID=2052
            left join T_PRD_RESTOCK a11 on t1.FBILLNO = a11.FBILLNO 
            LEFT JOIN T_PRD_RESTOCK_L A110 ON A110.FID=a11.FID AND A110.FLOCALEID=2052
            left join T_PRD_RETURNMTRL a12 on t1.FBILLNO = a12.FBILLNO 
            LEFT JOIN T_PRD_RETURNMTRL_L A120 ON A120.FID=A12.FID AND A120.FLOCALEID=2052
            left join T_SUB_FEEDMTRL a13 on t1.FBILLNO = a13.FBILLNO 
            LEFT JOIN T_SUB_FEEDMTRL_L A130 ON A130.FID=A13.FID AND A130.FLOCALEID=2052
            left join T_SUB_EXCONSUME a14 on t1.FBILLNO = a14.FBILLNO 
            LEFT JOIN T_SUB_EXCONSUME_L A140 ON A140.FID=A14.FID AND A140.FLOCALEID=2052
            left join T_SUB_PICKMTRL a15 on t1.FBILLNO = a15.FBILLNO 
            LEFT JOIN T_SUB_PICKMTRL_L A150 ON A150.FID=A15.FID AND A150.FLOCALEID=2052
            left join T_SUB_RETURNMTRL a16 on t1.FBILLNO = a16.FBILLNO 
            LEFT JOIN T_SUB_RETURNMTRL_L A160 ON A160.FID=A16.FID AND A160.FLOCALEID=2052
            left join T_SAL_OUTSTOCK a17 on t1.FBILLNO = a17.FBILLNO 
            left join T_SAL_RETURNSTOCK a18 on t1.FBILLNO = a18.FBILLNO 
            left join T_STK_StatusConvert a19 on t1.FBILLNO = a19.FBILLNO 
            left join T_STK_STKTRANSFERIN a20 on t1.FBILLNO = a20.FBILLNO 
            left join T_STK_ASSEMBLY a21 on t1.FBILLNO = a21.FBILLNO 
            ";
            sb.AppendFormat(strSql, tableName, strTable);
            DBUtils.Execute(this.Context, sb.ToString());
        }
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader header = base.GetReportHeaders(filter);
            header.AddChild("FNote", new LocaleValue("备注"));
            return header;
        }
        public override void CloseReport()
        {
            //删除临时表
            if (customRptTempTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<Kingdee.BOS.Contracts.IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customRptTempTableNames);
            base.CloseReport();
        }
    }
}
